package com.manage.tasks;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;

import com.manage.func.activity.dao.IActivityDao;
import com.manage.func.activity.model.ActivityBean;
import com.manage.util.PushUtil;
import com.manage.util.SpringApplicationContextHolder;

/**
 * 
 * 检查活动推送状态，推送活动
 * 
 * @author Administrator
 *
 */
public class ActivityPushTask extends TimerTask {


	@Override
	public void run() {
		try {
			IActivityDao mapper = SpringApplicationContextHolder.getSpringBeanByType(IActivityDao.class);
			while (mapper == null) {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
				}
				mapper = SpringApplicationContextHolder.getSpringBeanByType(IActivityDao.class);
			}
			System.out.println("ActivityPushTask run!");
			List<ActivityBean> beans = mapper.queryActListPushBean();
			if (beans != null) {
				for (ActivityBean bean : beans) {
					pushBean(bean, mapper);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 调用推送
	private void pushBean(ActivityBean bean, IActivityDao mapper) {
		if (bean != null) {
			boolean t = PushUtil.testSendPush(toMap(bean));
			if (t) {
				mapper.updateActPushStatus(bean.activityId);
			}
		}
	}

	private Map<String, String> toMap(ActivityBean bean) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("activityId", bean.activityId + "");
		map.put("title", bean.title);
		return map;
	}

}
